<template>
  <div>
    <el-dialog
      :title="isAdd ? '添加管理员' : '编辑管理员'"
      :visible.sync="isShow"
      center
      :close-on-press-escape="false"
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          prop="roleid"
          label="所属角色"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              v-for="item in rolelist"
              :label="item.rolename"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="username"
          label="用户名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            v-model="form.password"
            autocomplete="off"
            show-password
            placeholder="密码为空 则不做修改"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#409EFF"
            inactive-color="#DCDFE6"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit, userInfo } from "@/request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      dialogFormVisible: true,
      form: {
        roleid: "", //角色编号
        username: "", //管理员名称
        password: "", //密码
        status: 1, //状态  1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        roleid: [
          { required: true, message: "请选择所属角色", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      rolelist: "role/getRoleList",
    }),
  },
  mounted() {
    if (this.rolelist.length == 0) {
      this.getRole();
    }
  },
  methods: {
    ...mapActions({
      getRole: "role/changeRoleAction",
      getManager: "manager/changeManagerAction",
      getCount: "manager/changeCountAction",
    }),
    reset() {
      this.form = {
        roleid: "", //角色编号
        username: "", //管理员名称
        password: "", //密码
        status: 1, //状态   1正常2禁用   number类型
      };
      this.$refs.form.resetFields();
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(uid) {
      userInfo({
        uid,
      }).then((res) => {
        console.log(res, "查询一条数据");
        if (res.code == 200) {
          this.form = res.list;
          this.form.uid = uid;
          this.form.password = "";
        }
      });
    },
    comfirm() {
      this.$refs.form.validate((flag) => {
        console.log(flag);
        if (flag) {
          // console.log(this.form);
          if (this.isAdd) {
            userAdd(this.form).then((res) => {
              console.log(res, "管理员添加");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getManager();
                this.getCount();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            userEdit(this.form).then((res) => {
              console.log(res, "管理员编辑");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getManager();
                this.getCount();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
</style>
